package com.ts.main.admin.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ts.main.admin.dto.ProductDto;
import com.ts.main.admin.dto.ProductSearchPageDto;
import com.ts.main.admin.entity.ProductEntity;
import com.ts.main.admin.exception.RPage;
import com.ts.main.admin.mapper.ProductMapper;
import com.ts.main.admin.vo.ProductPartVo;
import com.ts.main.admin.vo.ProductVo;
import io.jsonwebtoken.lang.Collections;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
* @author lk
* @description 针对表【t_product(商品表)】的数据库操作Service实现
* @createDate 2025-06-09 00:56:13
*/
@AllArgsConstructor
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductEntity>{


    private final ProductFileServiceImpl productFileServiceImpl;


    public RPage<List<ProductVo>> getProductPage(ProductSearchPageDto dto) {
        Page<Object> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<ProductVo> list = baseMapper.getProductPage(dto);
        return RPage.ok(list, dto.getPage(), dto.getLimit(), page.getTotal());
    }

    public ProductVo getProductDetails(Integer productId) {
        return baseMapper.getProductDetails(productId);
    }

    @Transactional
    public Boolean saveProduct(ProductDto dto) {
        //新增商品
        baseMapper.saveProduct(dto);
        //商品图片
        productFileServiceImpl.deleteByProductId(dto.getProductId());
        if(!Collections.isEmpty(dto.getProductImageTwo())){
            productFileServiceImpl.saveList(dto.getProductId(),dto.getProductImageTwo());
        }
        return Boolean.TRUE;
    }

    @Transactional
    public Boolean updateProduct(ProductDto dto) {

        ProductEntity productEntity = new ProductEntity();
        BeanUtil.copyProperties(dto, productEntity);
        productEntity.setDefaultPrice(dto.getOriginalPrice());
        productEntity.setDefaultActualPrice(dto.getActualPrice());
        //修改商品
        updateById(productEntity);
        //商品图片
        productFileServiceImpl.deleteByProductId(dto.getProductId());
        if(!Collections.isEmpty(dto.getProductImageTwo())){
            productFileServiceImpl.saveList(dto.getProductId(),dto.getProductImageTwo());
        }
        return Boolean.TRUE;
    }

    @Transactional
    public Boolean deleteProduct(Integer productId) {
        LambdaUpdateWrapper<ProductEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(ProductEntity::getIsDeleted, true)
                .eq(ProductEntity::getProductId, productId);
        update(updateWrapper);
        return productFileServiceImpl.deleteByProductId(productId);
    }

    public List<ProductPartVo> getProductPartList() {
        LambdaQueryWrapper<ProductEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductEntity::getIsDeleted, Boolean.FALSE);
        return BeanUtil.copyToList(baseMapper.selectList(queryWrapper), ProductPartVo.class);
    }

}




